package com.wusuowei.shiro_jwt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wusuowei.shiro_jwt.annotation.CrudLog;
import com.wusuowei.shiro_jwt.common.enums.BusinessType;
import com.wusuowei.shiro_jwt.model.po.Category;
import com.wusuowei.shiro_jwt.model.po.Product;
import com.wusuowei.shiro_jwt.model.po.User;
import com.wusuowei.shiro_jwt.service.CategoryService;
import com.wusuowei.shiro_jwt.utils.R;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;







    /**
     * 查询分类信息
     * @return
     */
    @GetMapping
    public R findAll() {
        System.out.println(categoryService.list());
        return R.ok().setData(categoryService.list());
    }

    /**
     * 报错分类
     * @param category
     * @return
     */

    @RequiresRoles(value = {"admin","vip"},logical = Logical.OR)
    @Transactional
    @PostMapping("/add")
    @CrudLog(title = "添加分类", businessType = BusinessType.INSERT)
    public R save(@RequestBody Category category){
        if(category.getId()==null) {
            categoryService.saveOrUpdate(category);
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改分类
     * @param category
     * @return
     */

    @RequiresRoles(value = {"admin", "vip"}, logical = Logical.OR)
    @Transactional
    @PostMapping("/update")
    @CrudLog(title = "修改分类", businessType = BusinessType.UPDATE)
    public R update(@RequestBody Category category) {
        // 只有当分类ID非null时才视为更新操作
        if (category.getId() != null) {
            categoryService.updateById(category);
            return R.ok();
        }
        return R.error();
    }

    /**
     单个删除
     */
    @RequiresRoles("admin")
    @DeleteMapping("/{id}")
    @CrudLog(title = "删除分类",businessType = BusinessType.DELETE)
    public R delete(@PathVariable Integer id) {

        categoryService.removeById(id);

        return R.ok();
    }
    /**
     * 批量删除
     */
    @RequestMapping("/del/batch")
    @RequiresRoles("admin")
    @Transactional
    @CrudLog(title = "删除分类",businessType = BusinessType.DELETE)
    public R delete(@RequestBody List<Integer> ids){

        categoryService.removeBatchByIds(ids);

        return R.ok();
    }

    /**
     * @description 查找页面
     * @param pageNum 书籍页码
     * @param pageSize 页面大小
     * @param cname 分类名
     * @return
     */
    @RequiresRoles(value = {"admin","vip","sysUser"},logical = Logical.OR)//满足一个角色就能访问
    @GetMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(required = false) String cname
                                        ) {

        Page<Category> page = categoryService.findPage(new Page<>(pageNum, pageSize), cname);
        List<Category> records = page.getRecords();
//        List<Category> collect = records.stream().map(item -> {
//            item.setRole(roleService.getRoles(String.valueOf(item.getId())));
//            return item;
//        }).collect(Collectors.toList());
        page.setRecords(records);
        return R.ok().setData(page);
    }
}
